##########################################################
# John Henderson
# Replication Data for: "Hookworm Eradication as a Natural 
#   Experiment for Schooling and Voting in the American 
#   South", Forthcoming in Political Behavior, May 20, 2017
# 
##########################################################
#
#  table_4_campaign.R
#  -- file produces the placebo results for Table 4
#     before and after matching
#     for the binary RSC Campaign intervention 
#
##########################################################

rm(list=ls())

library(Matching)
library(AER)                           
library(foreign)

setwd('~/Dropbox/hookworm/replication')
source('funs/lower.bound.R')
            
load('data/hookwormData.Rdata') 

##########################################################  
# data required 
#  - mMat :: matching matrix used in matching and balancing
#  - oMat :: outcome matrix of county education and turnout rates
#  - zMat :: intervention matrix 
#  - pMat :: placebo matrix
# hookSouth :: combines all objects into one dataset, w/ additional covariates  
##########################################################

##########################################################
##########################################################
# BEFORE MATCHING
##########################################################
##########################################################
   
xtest=matrix(NA,3,6)    
ytest=matrix(NA,1,6)
placebo=list('xtest'=xtest,'ytest'=ytest)    
    
tr=zMat$z_campaign

# all ages
x0=pMat$ipums1900_6to17
   
placebo[[1]][1,1]=summary(lm(x0~tr))$coef[2,1]
placebo[[1]][1,2]=summary(lm(x0~tr))$coef[2,2] # prefer SE
placebo[[1]][1,3]=summary(lm(x0~tr))$coef[2,4]         
c(summary(lm(x0~tr))$r.s,length(summary(lm(x0~tr))$res))
  
# 6-13 ages  
x0=pMat$ipums1900_6to14
 
placebo[[1]][2,1]=summary(lm(x0~tr))$coef[2,1]
placebo[[1]][2,2]=summary(lm(x0~tr))$coef[2,2] # prefer SE 
placebo[[1]][2,3]=summary(lm(x0~tr))$coef[2,4] 
c(summary(lm(x0~tr))$r.s,length(summary(lm(x0~tr))$res))
        
# 14-17 ages
x0=pMat$ipums1900_15to17

placebo[[1]][3,1]=summary(lm(x0~tr))$coef[2,1]
placebo[[1]][3,2]=summary(lm(x0~tr))$coef[2,2] # prefer SE  
placebo[[1]][3,3]=summary(lm(x0~tr))$coef[2,4]  
c(summary(lm(x0~tr))$r.s,length(summary(lm(x0~tr))$res))
    
# y turnout 
y0=pMat$vote1912

placebo[[2]][1,1]=summary(lm(y0~tr))$coef[2,1]
placebo[[2]][1,2]=summary(lm(y0~tr))$coef[2,2] # prefer SE  
placebo[[2]][1,3]=summary(lm(y0~tr))$coef[2,4] 
c(summary(lm(y0~tr))$r.s,length(summary(lm(x0~tr))$res))

##########################################################
##########################################################
# AFTER MATCHING
##########################################################
##########################################################

mMat=bMat=mMat[,-c(1:3)][,1:19]

# pre-sets for 'lower.bound' fitfunc :: lower.bound only improves if total balance improves
floors=NULL					# :: allows to preset values at the floor overall
floor.values=NULL           # :: allows to preset values at the floor for each covariate
reweighted = -1 			# :: fits over a single summary measure of balance
exacts=array(0,ncol(mMat))  # :: exact matching for covariates 
exacts[1]=0    

starts=c(0.8639389,12.5338719,14.1059201,232.6227015,13.3530437,0.8680492,809.5765534,2.3393068,1.8030039,0.9325587,44.5785061,1.5006086,65.5576631,2.8099156,9.4050025,43.4617957,453.9672492,357.1535636,388.9175985)     

set.seed(1005)
genout=GenMatch(starting.values=starts,exact=exacts,MemoryMatrix=T,fit.func=lower.bound,Tr=tr,X=mMat,BalanceMatrix=bMat,M=1,pop.size=2,max.generations=5,wait.generations=3,hard.generation.limit=T,estimand="ATT",ties=T)

mout=(Match(Tr=tr,X=mMat,estimand="ATT",M=1,Weight.matrix=genout,exact=exacts,ties=T))
   
# all ages
x0=pMat$ipums1900_6to17

xtr=x0[mout$index.treated]
xct=x0[mout$index.control]
w=mout$weights
trt=tr[mout$index.treated]
tct=tr[mout$index.control]

xmat=c(xtr,xct)
zmat=c(trt,tct)
wmat=c(w,w) 
                                                                    
placebo[[1]][1,4]=summary(lm(xmat~zmat,weights=wmat))$coef[2,1]
placebo[[1]][1,5]=summary(lm(xmat~zmat,weights=wmat))$coef[2,2] # prefer SE
placebo[[1]][1,6]=summary(lm(xmat~zmat,weights=wmat))$coef[2,4]       
c(summary(lm(xmat~zmat,weights=wmat))$r.s,length(summary(lm(xmat~zmat,weights=wmat))$res))


# 6-13 ages
x0=pMat$ipums1900_6to14

xtr=x0[mout$index.treated]
xct=x0[mout$index.control]
w=mout$weights
trt=tr[mout$index.treated]
tct=tr[mout$index.control]

xmat=c(xtr,xct)
zmat=c(trt,tct)
wmat=c(w,w)
      
placebo[[1]][2,4]=summary(lm(xmat~zmat,weights=wmat))$coef[2,1]
placebo[[1]][2,5]=summary(lm(xmat~zmat,weights=wmat))$coef[2,2] # prefer SE
placebo[[1]][2,6]=summary(lm(xmat~zmat,weights=wmat))$coef[2,4]        
c(summary(lm(xmat~zmat,weights=wmat))$r.s,length(summary(lm(xmat~zmat,weights=wmat))$res))  

# 14-17 ages
x0=pMat$ipums1900_15to17

xtr=x0[mout$index.treated]
xct=x0[mout$index.control]
w=mout$weights
trt=tr[mout$index.treated]
tct=tr[mout$index.control]

xmat=c(xtr,xct)
zmat=c(trt,tct)
wmat=c(w,w)
         
placebo[[1]][3,4]=summary(lm(xmat~zmat,weights=wmat))$coef[2,1]
placebo[[1]][3,5]=summary(lm(xmat~zmat,weights=wmat))$coef[2,2] # prefer SE
placebo[[1]][3,6]=summary(lm(xmat~zmat,weights=wmat))$coef[2,4]     
c(summary(lm(xmat~zmat,weights=wmat))$r.s,length(summary(lm(xmat~zmat,weights=wmat))$res))  
 
# y turnout 

y0=pMat$vote1912      

ytr=y0[mout$index.treated]
yct=y0[mout$index.control]
w=mout$weights
trt=tr[mout$index.treated]
tct=tr[mout$index.control]

ymat=c(ytr,yct)
zmat=c(trt,tct)
wmat=c(w,w)
   
placebo[[2]][1,4]=summary(lm(ymat~zmat,weights=wmat))$coef[2,1]
placebo[[2]][1,5]=summary(lm(ymat~zmat,weights=wmat))$coef[2,2] # prefer SE
placebo[[2]][1,6]=summary(lm(ymat~zmat,weights=wmat))$coef[2,4]   
c(summary(lm(ymat~zmat,weights=wmat))$r.s,length(summary(lm(xmat~zmat,weights=wmat))$res))  

# print placebo[[1]]          
colnames(placebo[[2]])=colnames(placebo[[1]])=c('before','bf-SE','bf-pvalue','after','af-SE','af-pvalue')
rownames(placebo[[1]])=c('all','6-13','14-17')
rownames(placebo[[2]])=c('all')       

placebo

#End